using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace AreaBuilder.Forms {
    public partial class frmShops : Form {
        private readonly AreaHolder ah;
        private readonly int existingVNum = -1;

        public frmShops() {
            InitializeComponent();
        }

        public frmShops(AreaHolder ah, int value) {
            InitializeComponent();

            this.ah = ah;
            existingVNum = value;
        }

        private void trkSell_ValueChanged(object sender, EventArgs e) {
            lblPSellVal.Text = String.Format("{0}%", trkSell.Value);
        }

        private void trkBuy_ValueChanged(object sender, EventArgs e) {
            lblPBuyVal.Text = String.Format("{0}%", trkBuy.Value);
        }

        private void btnCancel_Click(object sender, EventArgs e) {
            Dispose();
        }

        private void btnOK_Click(object sender, EventArgs e) {
            Shop s = existingVNum >= 0
                         ? (from Shop shop in ah.area.Shops
                            where shop.VNum == existingVNum
                            select shop).Single()
                         : new Shop();

            s.VNum = getValue(lstOwner);
            s.First = getValue(lstItem1);
            s.Second = getValue(lstItem2);
            s.Third = getValue(lstItem3);
            s.Fourth = getValue(lstItem4);
            s.Fifth = getValue(lstItem5);

            s.Buy = trkBuy.Value;
            s.Sell = trkSell.Value;

            s.StartHour = lstStart.SelectedIndex;
            s.EndHour = lstEnd.SelectedIndex;

            if (existingVNum < 0)
                ah.area.Shops.Add(s);

            Dispose();
        }

        private void frmShops_Load(object sender, EventArgs e) {
            Funcs.fillComboType(lstItem1, "/types/shop/item/@type", "/types/shop/item", "");
            Funcs.fillComboType(lstItem2, "/types/shop/item/@type", "/types/shop/item", "");
            Funcs.fillComboType(lstItem3, "/types/shop/item/@type", "/types/shop/item", "");
            Funcs.fillComboType(lstItem4, "/types/shop/item/@type", "/types/shop/item", "");
            Funcs.fillComboType(lstItem5, "/types/shop/item/@type", "/types/shop/item", "");

            for (int i = 0; i < ah.area.Mobs.Count; i++) {
                var mob = (Mobile) ah.area.Mobs[i];
                lstOwner.Items.Add(String.Format("#{0} - {1}", mob.VNum, mob.Name));
            }

            for (int i = 0; i < 24; i++) {
                lstStart.Items.Add(String.Format("{0}:00", i));
                lstEnd.Items.Add(String.Format("{0}:00", i));
            }

            if (existingVNum < 0) {
                lstOwner.SelectedIndex = 0;
                lstItem1.SelectedIndex = 0;
                lstItem2.SelectedIndex = 0;
                lstItem3.SelectedIndex = 0;
                lstItem4.SelectedIndex = 0;
                lstItem5.SelectedIndex = 0;
                lstStart.SelectedIndex = 0;
                lstEnd.SelectedIndex = 0;
            } else {
                Shop shop = (from Shop s in ah.area.Shops
                             where s.VNum == existingVNum
                             select s).Single();

                lstOwner.SelectedIndex = Funcs.getComboIndex(lstOwner, shop.VNum.ToString());
                lstItem1.SelectedIndex = Funcs.getComboIndex(lstItem1, shop.First.ToString());
                lstItem2.SelectedIndex = Funcs.getComboIndex(lstItem2, shop.Second.ToString());
                lstItem3.SelectedIndex = Funcs.getComboIndex(lstItem3, shop.Third.ToString());
                lstItem4.SelectedIndex = Funcs.getComboIndex(lstItem4, shop.Fourth.ToString());
                lstItem5.SelectedIndex = Funcs.getComboIndex(lstItem5, shop.Fifth.ToString());

                trkBuy.Value = shop.Buy;
                trkSell.Value = shop.Sell;

                lstStart.SelectedIndex = shop.StartHour;
                lstEnd.SelectedIndex = shop.EndHour;
            }
        }

        private static int getValue(ComboBox combo) {
            var re = new Regex(@"(?<number>\d+)\s*[\-:]");
            Match m = re.Match(combo.SelectedItem.ToString());

            if (m.Success)
                return Int32.Parse(m.Groups["number"].Value);

            return 0;
        }

        private void btnPrev_Click(object sender, EventArgs e) {
            try {
                int currVnum = getValue(lstOwner);
                IEnumerable<int> query = from Shop s in ah.area.Shops
                                         where s.VNum < currVnum
                                         orderby s.VNum descending
                                         select s.VNum;
                int newArray = query.First();

                Form newForm = new frmShops(ah, newArray) {MdiParent = MdiParent};

                newForm.Location = new Point((MdiParent.Size.Width - newForm.Size.Width)/2 + 40,
                                             (MdiParent.Size.Height - newForm.Size.Height)/2 - 60);
                newForm.Show();

                Dispose();
            } catch (Exception) {
                MessageBox.Show("There are no more mobs to show!", "Whoops!", MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation);
            }
        }

        private void btnNext_Click(object sender, EventArgs e) {
            try {
                int currVnum = getValue(lstOwner);
                IEnumerable<int> query = from Shop s in ah.area.Shops
                                         where s.VNum > currVnum
                                         orderby s.VNum ascending
                                         select s.VNum;
                int newArray = query.First();

                Form newForm = new frmShops(ah, newArray) {MdiParent = MdiParent};

                newForm.Location = new Point((MdiParent.Size.Width - newForm.Size.Width)/2 + 40,
                                             (MdiParent.Size.Height - newForm.Size.Height)/2 - 60);
                newForm.Show();

                Dispose();
            } catch (Exception) {
                MessageBox.Show("There are no more mobs to show!", "Whoops!", MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation);
            }
        }
    }
}